package com.weizhu.swufer.testprovider.client;

import com.google.protobuf.ByteString;
import com.weizhu.swufer.core.client.GenericService;
import com.weizhu.swufer.core.rpc.RpcRequest;
import com.weizhu.swufer.core.rpc.RpcResponse;
import com.weizhu.swufer.core.server.SwuferBootstrap;
import com.weizhu.swufer.core.util.ProtoUtil;
import com.weizhu.swufer.grpc.GrpcCommonServiceGrpc;
import com.weizhu.swufer.grpc.Request;
import com.weizhu.swufer.grpc.Response;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class TestClient {


    public static void main(String[] args) throws Exception {
        testGeneric();
        new CountDownLatch(1).await();
    }

    public static void testGeneric() throws Exception {
        SwuferBootstrap bootstrap = SwuferBootstrap.newBuilder().registry("42.193.247.5:2181")
                .port(8001).scanRpcService("com.weizhu.swufer.testprovider.demo").buildBootstrap();
        bootstrap.start();
    }



    public static void test1(){
        ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 8001)
                .usePlaintext().build();
        GrpcCommonServiceGrpc.GrpcCommonServiceBlockingStub stub = GrpcCommonServiceGrpc.newBlockingStub(channel);
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setMethodName("hello");
        rpcRequest.setInterfaceName("com.weizhu.swufer.test.TestService");
        Object[] params = new Object[]{"zyy"};
        rpcRequest.setArgs(params);
        ByteString bytes = ByteString.copyFrom(ProtoUtil.serialize(rpcRequest));
        Request request = Request.newBuilder().setRequest(bytes).build();
        Response response = stub.callService(request);
        RpcResponse rpcResponse = ProtoUtil.deserialize(response.getResponse().toByteArray(), RpcResponse.class);
        System.out.println(rpcResponse);
    }
}
